/**
 * Created by gongxun on 16/11/18.
 */

var KwxHallLayer = KwxBaseLayer.extend({
    mKwxHornHelper : null,
    mKwxLoginLayer : null,
    Button_create : null,
    Button_join : null,
    mLastVisible : null,
    Image_musume : null,
    mPlayedKaichangbai : null,

    mKwxRoomSelectLayer : null,

    mLastUser : null,
    ctor : function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxHallLayer.json");
        this.mLastVisible = true;

        this.mKwxHornHelper = new KwxHornHelper(mo.seekWidgetByName(this.ccsNode, "Image_horn"));

        this.loadUI();
        this.loadJoinCreateButton();

        //museme
        this.Image_musume = mo.seekWidgetByName(this.ccsNode, "Image_musume");
        this.musemeAnim();

        //设置动画
        mo.seekWidgetByName(this.ccsNode, "Button_setting").runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.moveTo(3, cc.p(mo.curSize.x - 50, -54)),
                    cc.moveTo(3, cc.p(mo.curSize.x - 50, -40)))
            ));
        mo.seekWidgetByName(this.ccsNode, "Button_share").runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.moveTo(3.5, cc.p(mo.curSize.x - 150, -54)),
                    cc.moveTo(2.5, cc.p(mo.curSize.x - 150, -40)))
            )
        );

        //头像
        if (cc.sys.isNative){
            var Image_userInfoBg = mo.seekWidgetByName(this.ccsNode, "Image_userInfoBg");

            var Image_head = mo.seekWidgetByName(this.ccsNode, "Image_head");
            Image_head.setPosition(cc.p(0, 0));
            Image_head.removeFromParent();

            var clipper = new cc.ClippingNode();
            clipper.setInverted(false);
            clipper.setAlphaThreshold(0.5);

            var spirte = new cc.Sprite("res/ccs/hall_second/hall/head.png");
            clipper.setStencil(spirte);

            clipper.addChild(Image_head);
            // mo.gameHelper.loadImageForUrl(Image_head, infos[i].user_info.faceUrl);
            // mo.gameHelper.loadImageForUrl(Image_head, "http://wx1.sinaimg.cn/mw690/891c9115ly1ffxdq56dhfj20hs0zk0tm.jpg");

            clipper.setPosition(cc.p(50, 50));

            Image_userInfoBg.addChild(clipper, -1);
        }
    },

    onEnter : function(){
        this._super();
        mo.gameHelper.fixIosText(this.ccsNode);

        this.updateUserInfo();
        mo.eventHelper.add("updateUserInfo", this, function(){
            this.updateUserInfo();
        });

        mo.eventHelper.add("PushMessageMarquee", this, function(contents){
            this.mKwxHornHelper.setHornText(contents);
        });
        this.mKwxHornHelper.startAction();

        mo.eventHelper.add("showRoomYilaiLayer", this, function(){
            this.addChild(new KwxRoomDaikaiLayer());
        });

        mo.eventHelper.add("showNotFriendLayer", this, function(){
            this.addChild(new KwxNotFriendLayer());
        });

        this.updateUIForIosCheck();
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this.mKwxHornHelper.endAction();
        this._super();
    },

    setVisible : function(isVisible){
        this._super(isVisible);

        if (isVisible == true && this.mLastVisible == false){

            this.scheduleOnce(function(){
                mo.soundHelper.playMusicHall();
            }, 1);

            //进场动画
            this.intoAnim();
        }else{
        }
        this.mLastVisible = isVisible;
    },

    intoLoginLayer : function(){
        this.mLastVisible = false;

        if (!this.mKwxLoginLayer){
            this.mKwxLoginLayer = new KwxLoginLayer();
            this.addChild(this.mKwxLoginLayer);
        }
    },

    closeLoginLayer : function(){
        this.mLastVisible = true;

        if (this.mKwxLoginLayer){
            this.mKwxLoginLayer.removeFromParent();
            this.mKwxLoginLayer = null;
        }
    },

    loadUI : function(){

        //房卡
        mo.seekWidgetByName(this.ccsNode, "Image_roomCardBg").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                //TODO test wxPay
                this.addChild(new KwxWxShopLayer());
                return;
                if (mo.gameHelper.isIosCheck){
                    this.addChild(new KwxStoreLayer());
                }else{
                    // var showMsg = "购买房卡，申请代理请联系我们:\n\nqixinqipai(微信)\n206132618(QQ)";
                    // mo.showMsgBox(showMsg);
                    // mo.userMessage.getUserMessageIds();
                    this.addChild(new KwxWxHelperLayer());
                }
            }
        }, this);

        //我要房卡
        mo.seekWidgetByName(this.ccsNode, "Button_woyaofangka").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                this.addChild(new KwxWxHelperLayer());
            }
        }, this);

        //设置
        mo.seekWidgetByName(this.ccsNode, "Button_setting").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                this.addChild(new KwxHallSettingLayer());
            }
        }, this);

        //分享
        mo.seekWidgetByName(this.ccsNode, "Button_share").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                mo.gameHelper.onWxShareWeb("《" + mo.gameHelper.projectName + "卡五星》", "掌上卡五星，随时随地组局，绝无外挂，趣味家乡方言，好玩看得见", mo.gameHelper.appHomeUrl);
            }
        }, this);

        //帮助
        mo.seekWidgetByName(this.ccsNode, "Button_help").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                this.addChild(new KwxHelperLayer());
            }
        }, this);

        //牌局记录
        mo.seekWidgetByName(this.ccsNode, "Button_record").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                this.addChild(new KwxRecordLayer());
            }
        }, this);

        //礼包
        mo.seekWidgetByName(this.ccsNode, "Button_gift").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                this.addChild(new KwxGiftLayer());
            }
        }, this);

        //兑换
        if (mo.gameHelper.projectCode == 0){
            mo.seekWidgetByName(this.ccsNode, "Button_giftExchange").addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                    this.addChild(new KwxGiftExchangeLayer());
                }
            }, this);
        }

        this.mKwxRoomSelectLayer = new KwxRoomSelectLayer();
        this.addChild(this.mKwxRoomSelectLayer);
        this.mKwxRoomSelectLayer.setVisible(false);
    },

    updateUserInfo : function () {
        var user = mo.gameUserInfo.user;

        //延迟3秒获取地理位置对应的微信客服
        if (cc.sys.isNative && user != null && this.mLastUser == null) {
            var that = this;
            this.scheduleOnce(function () {
                var localCHSName = mo.httpClient.sendLocationNameToHtml(function (isSuccess, url) {
                    if (isSuccess) {
                        cc.log("找到对应的微信号:" + url);
                        if (localCHSName != ""){
                            that.mKwxHornHelper.customerWX = "[" + localCHSName + "]地区客服微信:[" + url + "]";
                        }else{
                            that.mKwxHornHelper.customerWX = "欢迎联系客服微信:[" + url + "]";
                        }
                    } else {
                        cc.log("没有找到推荐微信号");
                    }
                });
            }, 3);
        }

        if (user){
            //head
            mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(this.ccsNode, "Image_head"), user.faceUrl);

            var Text_nick = mo.seekWidgetByName(this.ccsNode, "Text_nick");
            mo.gameHelper.setTextString(Text_nick, user.nickname);

            var Text_uid = mo.seekWidgetByName(this.ccsNode, "Text_uid");
            mo.gameHelper.setTextString(Text_uid, "ID:" + user.userid);

            //roomcard
            var Text_roomCard = mo.seekWidgetByName(this.ccsNode, "Text_roomCard");
            mo.gameHelper.setTextString(Text_roomCard, mo.gameUserInfo.getRoomCard() + "");

            this.mLastUser = user;
        }
    },

    loadJoinCreateButton : function(){
        this.Button_create = mo.seekWidgetByName(this.ccsNode, "Button_create");
        this.Button_join = mo.seekWidgetByName(this.ccsNode, "Button_join");

        //创建
        this.Button_create.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)){
                // this.addChild(new KwxRoomCreateLayer());
                this.mKwxRoomSelectLayer.setVisible(true);
            }
        }, this);

        //加入
        this.Button_join.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)){
                this.addChild(new KwxRoomJoinLayer());
            }
        }, this);
    },

    setNotiveGiftPointer : function(isShow){
        mo.seekWidgetByName(this.ccsNode, "Image_giftRedPointer").setVisible(isShow);
    },

    intoAnim : function(){
        if (this.mPlayedKaichangbai != true){
            mo.soundHelper.playeKaichangbai();
            this.mPlayedKaichangbai = true;
        }
        mo.soundHelper.playIntoHallEffect();

        var Panel_top = mo.seekWidgetByName(this.ccsNode, "Panel_top");
        var Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        var Panel_buttonLeft = mo.seekWidgetByName(this.ccsNode, "Panel_buttonLeft");
        var Panel_buttonRight = mo.seekWidgetByName(this.ccsNode, "Panel_buttonRight");

        Panel_top.setPosition(cc.p(0, mo.curSize.y + 150));
        Panel_center.setPosition(cc.p(mo.curSize.x, mo.curSize.y / 2));
        Panel_buttonLeft.setPosition(cc.p(-mo.curSize.x / 2, 0));
        Panel_buttonRight.setPosition(cc.p(mo.curSize.x + mo.curSize.x / 2, 0));

        Panel_top.stopAllActions();
        Panel_center.stopAllActions();
        Panel_buttonLeft.stopAllActions();
        Panel_buttonRight.stopAllActions();

        var delayTime = 0.5;
        Panel_top.runAction(cc.sequence(cc.moveTo(delayTime, cc.p(0, mo.curSize.y))));
        Panel_center.runAction(cc.sequence(cc.moveTo(delayTime, cc.p(mo.curSize.x / 2, mo.curSize.y / 2))));
        Panel_buttonLeft.runAction(cc.sequence(cc.moveTo(delayTime, cc.p(0, 0))));
        Panel_buttonRight.runAction(cc.sequence(cc.moveTo(delayTime, cc.p(mo.curSize.x, 0))));
    },

    musemeAnim : function(){
        this.Image_musume.stopAllActions();
        var time = 10 + Math.random() * 5;
        var scale = 0.95 + Math.random() * 0.1;
        var that = this;
        this.Image_musume.runAction(
            cc.sequence(
                cc.scaleTo(time, scale),
                cc.callFunc(function(){
                    that.musemeAnim()
                }))
        );
    },

    updateUIForIosCheck : function(){
        if (mo.gameHelper.isIosCheck){

            mo.seekWidgetByName(this.ccsNode, "Button_gift").setVisible(false);
            mo.seekWidgetByName(this.ccsNode, "Button_woyaofangka").setVisible(false);
            mo.seekWidgetByName(this.ccsNode, "Button_share").setVisible(false);
            mo.seekWidgetByName(this.ccsNode, "Button_giftExchange").setVisible(false);
        }else{

            mo.seekWidgetByName(this.ccsNode, "Button_gift").setVisible(true);
            mo.seekWidgetByName(this.ccsNode, "Button_woyaofangka").setVisible(true);
            mo.seekWidgetByName(this.ccsNode, "Button_share").setVisible(true);
            mo.seekWidgetByName(this.ccsNode, "Button_giftExchange").setVisible(true);
        }
    }
});